package com.luo.locationapp.utils;

import com.luo.locationapp.R;
import com.luo.locationapp.global.GlobalContents;
import com.luo.locationapp.link.AsynImageLoader;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomDialog{

	public static Dialog createCustomProgressDlg(Context context, String title, int width, int height){
		Dialog dialog = new Dialog(context);  
		
        
		int resource = R.layout.busy_dialog;
		
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        if(title == null || "".equals(title)){
        	dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
        	lp.height = (int) (width * 0.2);
        }
        else{
        	dialog.setTitle(title);
        	lp.height = (int) (height * 0.3);
        }
        
        
        
        lp.width = (int) (GlobalContents.screen_width * 0.6);
        //lp.alpha = 0.6f;
        
        dialogWindow.setAttributes(lp);
        dialog.setContentView(resource);
        dialog.setCancelable(false);
        dialog.show();
        
        return dialog;
	}
	
	public static void createCustomMessageDlg(Context context, String message, final OnClickListener commitListener){
		 /*AlertDialog.Builder builder =new AlertDialog.Builder(context);
	        
	     LayoutInflater inflater = LayoutInflater.from(context);
	     View layout = inflater.inflate(resource, null);
	     builder.setTitle("��¼");
	     builder.setView(layout);
	     builder.setPositiveButton("ȷ��",null);
	     builder.create();
	     builder.show();*/
		 int resource = R.layout.message_dialog;
		
	     final Dialog dialog = new Dialog(context);  
			
	     Window dialogWindow = dialog.getWindow();
	     dialogWindow.setGravity(Gravity.CENTER);
	     dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
	     dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
	     
	     WindowManager.LayoutParams lp = dialogWindow.getAttributes();
	     
	     lp.width = (int) (GlobalContents.screen_width * 0.5);
	     //lp.alpha = 0.6f;
	        
	     dialogWindow.setAttributes(lp);
	     dialog.setContentView(resource);
	     dialog.setCancelable(false);
	     TextView tv_message = (TextView) dialog.findViewById(R.id.message_dialog_messages);
	     Button btn_commit = (Button) dialog.findViewById(R.id.message_dialog_bt_commit);
	     Button btn_cancel = (Button) dialog.findViewById(R.id.message_dialog_bt_cancel);
	     
	     if(message != null){
	    	 tv_message.setText(message);
	     }
    	 btn_commit.setOnClickListener(commitListener);
    	 btn_commit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				if(commitListener != null)
					commitListener.onClick(arg0);
			}
		});
	     
	     btn_cancel.setOnClickListener(new OnClickListener() {
	    	 @Override
	    	 public void onClick(View arg0) {
	    		 dialog.dismiss();
	    	 }
	     });
	     dialog.show();
	}
	
	public static void createCustomDetailImgDlg(Context context,String path) {
		/*
		 * AlertDialog.Builder builder =new AlertDialog.Builder(context);
		 * 
		 * LayoutInflater inflater = LayoutInflater.from(context); View layout =
		 * inflater.inflate(resource, null); builder.setTitle("��¼");
		 * builder.setView(layout); builder.setPositiveButton("ȷ��",null);
		 * builder.create(); builder.show();
		 */
		int resource = R.layout.image_detail_dialog;
		
		AsynImageLoader asynImageLoader = new AsynImageLoader();
		ScaleAnimation animation = new ScaleAnimation(0f,1f,0f,1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
		animation.setDuration(300);
		final Dialog dialog = new Dialog(context);

		Window dialogWindow = dialog.getWindow();
		dialogWindow.setGravity(Gravity.CENTER);
		dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
		dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);

		WindowManager.LayoutParams lp = dialogWindow.getAttributes();

		lp.width = (int) (GlobalContents.screen_width);
		lp.height = (int) (GlobalContents.screen_height);
		// lp.alpha = 0.6f;

		dialogWindow.setAttributes(lp);
		dialog.setContentView(resource);
		//dialog.setCancelable(false);
		ImageView imageView = (ImageView) dialog.findViewById(R.id.image_detail_dialog_src);
		final Bitmap bitmap = PicUtil.getLoacalBitmap(path);

		if (imageView != null) {
			//asynImageLoader.showOriginalSizeImageAsyn(imageView, path, R.drawable.default_img);
			imageView.setImageBitmap(bitmap);
		}
		imageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				bitmap.recycle();
				dialog.dismiss();
			}
		});
		asynImageLoader.setRunning(false);
		imageView.startAnimation(animation);
		dialog.show();
	}
}
